<?php

namespace app\common\service\trainer;


use app\common\enums\core\AuditStateEnum;
use app\common\enums\TrainerAuditTypeEnum;
use app\common\enums\TrainerStatusEnum;
use app\common\model\banxi\trainer\TrainerAudit;
use app\common\service\ApiService;
use think\exception\ValidateException;

class TrainerAuditService extends ApiService
{
    public function auditApply(string $auditUserId, string $auditUserName, array $data)
    {
        $auditId = $data['auditId'];
        $trainerAuditModel = new TrainerAudit();
        $trainerAudit = $trainerAuditModel->where(['id' => $auditId])->find();
        if (!$trainerAudit) {
            throw new ValidateException('审核记录不存在');
        }
        if ($trainerAudit['applyState'] != AuditStateEnum::E1) {
            throw new ValidateException('该申请已被审核');
        }
        $result = $data['result'];
        $cnt = $trainerAuditModel->updateAuditById($auditId, $auditUserId, $auditUserName, $result, $data['auditReason']);
        if (!$cnt) {
            throw new ValidateException('更新审核状态失败，请稍后重试');
        }
        if ($result) {
            $auditType = $trainerAudit['auditType'];
            $trainer = $trainerAudit;
            $trainer['id'] = null;
            switch ($auditType) {
                case TrainerAuditTypeEnum::ADD:
                    $trainer['status'] = TrainerStatusEnum::E3;
                    //添加
                    break;
                case TrainerAuditTypeEnum::EDIT:
                    $trainer['id'] = $trainerAudit['id'];
                    //编辑
                    break;
            }
        }
    }

}